package es.udc.fic.hamNeggs.dao;

import es.udc.fic.hamNeggs.manager.MeetingCommentManager;
import es.udc.fic.hamNeggs.manager.ProjectManager;
import es.udc.fic.hamNeggs.manager.UserManager;
import es.udc.fic.hamNeggs.pojo.Meeting;
import es.udc.fic.hamNeggs.pojo.Meeting.MeetingType;
import es.udc.fic.hamNeggs.pojo.MeetingComment;
import es.udc.fic.hamNeggs.pojo.User;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class InputMeeting {
    private String                        id;

    private String                        type;

    private String                        date;

    private String                        project;

    private String                        meetingLeader;

    private String                        stringMeetingLeader;

    private String                        stringAttendees;

    private ArrayList<String>             attendees = new ArrayList<String>();

    private final HashMap<String, String> comments  = new HashMap<String, String>();

    public String getId() {
        return id;
    }

    public void setId(final String id) {
        this.id = id;
    }

    public String getType() {
        return type;
    }

    public void setType(final String type) {
        this.type = type;
    }

    public String getDate() {
        return date;
    }

    public void setDate(final String date) {
        this.date = date;
    }

    public String getProject() {
        return project;
    }

    public void setProject(final String project) {
        this.project = project;
    }

    public String getMeetingLeader() {
        return meetingLeader;
    }

    public void setMeetingLeader(final String meetingLeader) {
        this.meetingLeader = meetingLeader;
    }

    public ArrayList<String> getAttendees() {
        return attendees;
    }

    public void setAttendees(final ArrayList<String> attendees) {
        this.attendees = attendees;
    }

    public String getStringAttendees() {
        return stringAttendees;
    }

    public void setStringAttendees(final String stringAttendees) {
        this.stringAttendees = stringAttendees;
    }

    public String getStringMeetingLeader() {
        return stringMeetingLeader;
    }

    public void setStringMeetingLeader(final String stringMeetingLeader) {
        this.stringMeetingLeader = stringMeetingLeader;
    }

    public Meeting toMeeting() {
        final Meeting myMeeting = new Meeting();
        final ProjectManager prjmng = new ProjectManager();
        final UserManager usrmng = new UserManager();
        final MeetingCommentManager cmtmng = new MeetingCommentManager();
        if (id != null && id != "")
            myMeeting.setId(new Long(this.id));
        if (type == null || MeetingType.valueOf(type) == null)
            return null;
        myMeeting.setType(MeetingType.valueOf(type));
        if (date == null || date == "")
            return null;
        myMeeting.setDate(Date.valueOf(date));
        if (project != null && !project.equalsIgnoreCase(""))
            myMeeting.setProject(prjmng.getProject(new Long(project)));
        if (meetingLeader != null && !meetingLeader.equalsIgnoreCase(""))
            myMeeting.setMeetingLeader(usrmng.getUser(new Long(meetingLeader)));
        if (attendees != null && !attendees.isEmpty())
            for (final String user : attendees)
                myMeeting.setAttendee(usrmng.getUser(new Long(user)));
        if (comments != null && !comments.isEmpty()) {
            for (Map.Entry<String, String> entry : comments.entrySet()) {
                myMeeting.setOneComment(cmtmng.getMeetingComment(new Long(entry.getKey())));
            }
        }

        prjmng.close();
        usrmng.close();
        cmtmng.close();

        return myMeeting;
    }

    public void setMeeting(final Meeting meeting) {
        this.id = meeting.getId().toString();
        if (meeting.getDate() != null)
            this.date = meeting.getDate().toString();
        if (meeting.getMeetingLeader() != null) {
            this.meetingLeader = meeting.getMeetingLeader().getId().toString();
            this.stringMeetingLeader = meeting.getMeetingLeader().getFullName();
        }
        if (meeting.getType() != null)
            this.type = meeting.getType().getString();
        if (meeting.getProject() != null)
            this.project = meeting.getProject().getId().toString();
        if (meeting.getAttendees() != null && !meeting.getAttendees().isEmpty())
            for (final User user : meeting.getAttendees())
                this.attendees.add(user.getId().toString());
        if (meeting.getComments() != null && !meeting.getComments().isEmpty())
            for (final MeetingComment mtgcmt : meeting.getComments()) {
                comments.put(mtgcmt.getId().toString(), mtgcmt.getComment());
            }
    }

    public void setMoreAttendees(final ArrayList<String> atendees) {
        this.attendees.addAll(atendees);
    }

    public boolean attends(final String user) {
        for (final String a : attendees)
            if (a.equalsIgnoreCase(user))
                return true;
        return false;

    }

    public HashMap<String, String> getComments() {
        return comments;
    }

}
